<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="指令流水号" prop="cmdSerial">
        <el-input
          v-model="queryParams.cmdSerial"
          placeholder="请输入指令流水号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卫星ID" prop="satId">
        <el-input
          v-model="queryParams.satId"
          placeholder="请输入卫星ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板ID" prop="templateId">
        <el-input
          v-model="queryParams.templateId"
          placeholder="请输入模板ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划发送时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeSendTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="实际执行时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeExecuteTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="指令状态" prop="cmdStatus">
        <el-select v-model="queryParams.cmdStatus" placeholder="请选择指令状态" clearable>
          <el-option
            v-for="dict in kmx_cmd_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重试次数" prop="retryCount">
        <el-input
          v-model="queryParams.retryCount"
          placeholder="请输入重试次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送地面站" prop="groundStation">
        <el-input
          v-model="queryParams.groundStation"
          placeholder="请输入发送地面站"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['zhilingguanli:uplink:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zhilingguanli:uplink:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zhilingguanli:uplink:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['zhilingguanli:uplink:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="uplinkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="指令ID" align="center" prop="uplinkId" />
      <el-table-column label="指令流水号" align="center" prop="cmdSerial" />
      <el-table-column label="卫星ID" align="center" prop="satId" />
      <el-table-column label="模板ID" align="center" prop="templateId" />
      <el-table-column label="实际指令内容" align="center" prop="cmdContent" />
      <el-table-column label="计划发送时间" align="center" prop="sendTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际执行时间" align="center" prop="executeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.executeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="指令状态" align="center" prop="cmdStatus">
        <template #default="scope">
          <dict-tag :options="kmx_cmd_status" :value="scope.row.cmdStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="重试次数" align="center" prop="retryCount" />
      <el-table-column label="发送地面站" align="center" prop="groundStation" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['zhilingguanli:uplink:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['zhilingguanli:uplink:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改上行指令对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="uplinkRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="指令流水号" prop="cmdSerial">
          <el-input v-model="form.cmdSerial" placeholder="请输入指令流水号" />
        </el-form-item>
        <el-form-item label="卫星ID" prop="satId">
          <el-input v-model="form.satId" placeholder="请输入卫星ID" />
        </el-form-item>
        <el-form-item label="模板ID" prop="templateId">
          <el-input v-model="form.templateId" placeholder="请输入模板ID" />
        </el-form-item>
        <el-form-item label="实际指令内容">
          <editor v-model="form.cmdContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="计划发送时间" prop="sendTime">
          <el-date-picker clearable
            v-model="form.sendTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择计划发送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际执行时间" prop="executeTime">
          <el-date-picker clearable
            v-model="form.executeTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择实际执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="指令状态" prop="cmdStatus">
          <el-select v-model="form.cmdStatus" placeholder="请选择指令状态">
            <el-option
              v-for="dict in kmx_cmd_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重试次数" prop="retryCount">
          <el-input v-model="form.retryCount" placeholder="请输入重试次数" />
        </el-form-item>
        <el-form-item label="发送地面站" prop="groundStation">
          <el-input v-model="form.groundStation" placeholder="请输入发送地面站" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-select v-model="form.delFlag" placeholder="请选择删除标志">
            <el-option
              v-for="dict in is_deleted"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Uplink">
import { listUplink, getUplink, delUplink, addUplink, updateUplink } from "@/api/zhilingguanli/uplink";

const { proxy } = getCurrentInstance();
const { is_deleted, kmx_cmd_status } = proxy.useDict('is_deleted', 'kmx_cmd_status');

const uplinkList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeSendTime = ref([]);
const daterangeExecuteTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    cmdSerial: null,
    satId: null,
    templateId: null,
    sendTime: null,
    executeTime: null,
    cmdStatus: null,
    retryCount: null,
    groundStation: null,
  },
  rules: {
    cmdSerial: [
      { required: true, message: "指令流水号不能为空", trigger: "blur" }
    ],
    satId: [
      { required: true, message: "卫星ID不能为空", trigger: "blur" }
    ],
    cmdContent: [
      { required: true, message: "实际指令内容不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询上行指令列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeSendTime && '' != daterangeSendTime) {
    queryParams.value.params["beginSendTime"] = daterangeSendTime.value[0];
    queryParams.value.params["endSendTime"] = daterangeSendTime.value[1];
  }
  if (null != daterangeExecuteTime && '' != daterangeExecuteTime) {
    queryParams.value.params["beginExecuteTime"] = daterangeExecuteTime.value[0];
    queryParams.value.params["endExecuteTime"] = daterangeExecuteTime.value[1];
  }
  listUplink(queryParams.value).then(response => {
    uplinkList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    uplinkId: null,
    cmdSerial: null,
    satId: null,
    templateId: null,
    cmdContent: null,
    sendTime: null,
    executeTime: null,
    cmdStatus: null,
    retryCount: null,
    groundStation: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("uplinkRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeSendTime.value = [];
  daterangeExecuteTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.uplinkId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加上行指令";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _uplinkId = row.uplinkId || ids.value
  getUplink(_uplinkId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改上行指令";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["uplinkRef"].validate(valid => {
    if (valid) {
      if (form.value.uplinkId != null) {
        updateUplink(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUplink(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _uplinkIds = row.uplinkId || ids.value;
  proxy.$modal.confirm('是否确认删除上行指令编号为"' + _uplinkIds + '"的数据项？').then(function() {
    return delUplink(_uplinkIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('zhilingguanli/uplink/export', {
    ...queryParams.value
  }, `uplink_${new Date().getTime()}.xlsx`)
}

getList();
</script>
